<?php

require_once 'include/language_manager.inc.php';
Language_Manager::getInstance();

$meta["title"] = L::meta_recipe_day_title;
$meta["description"] = L::meta_recipe_day_description;
$meta["keywords"] = L::meta_recipe_day_keywords;

require "include/template_manager.inc.php";
require "include/image_manager.inc.php";

$myTemplate = new Template_Manager("lista_ricette", null, true, 7, $meta);
$query = "SELECT COUNT(*) AS conta FROM ricette";
$num = 10;
$range = 2;

$prefix = "/";
$labelRecipes = "ricette";
$labelRecipe = "ricetta";
if (getLanguage() == 'en') {
    $prefix = "/en/";
    $labelRecipes = "recipes";
    $labelRecipe = "recipe";
}

if ($data = DB_Manager::getInstance()->query($query)) {
    $row = $data->fetch_array(MYSQLI_ASSOC);
    $last = (int) (($row['conta'] - 1) / $num);
}
if (!isset($_REQUEST['pagina']) || ((int) $_REQUEST['pagina']) == 1) {
    $myTemplate->setCanonicalUrl($prefix . $labelRecipes . "/");

    $pagina = 0;
} else {
    $myTemplate->setCanonicalUrl($prefix . $labelRecipes . "/" . $_REQUEST['pagina'] . "/");
    $pagina = (int) $_REQUEST['pagina'] - 1;
}

$start = $num * $pagina;
$query = "SELECT `r`.`Id` AS `id`, `r`.`titolo` AS `titolo`, `r`.`titolo_en` AS `titolo_en`,`r`.`Tempo` AS `tempo`,`r`.`Difficolta` AS `difficolta`,"
        . "`r`.`Data` as `data`,`i`.`uri` as `uri`, concat(`p`.`nome`,' ',`p`.`cognome`) AS `autore`,"
        . "`p`.`titolo` AS `prenome`,`i`.`descrizione` AS `descrizione`,`i`.`descrizione_en` AS `descrizione_en`,`r`.`persone` as `persone` "
        . "FROM ((`ricette` `r` join `immagine` `i`) join `professori` `p`) "
        . "WHERE (`r`.`immagine` = `i`.`id`) and (`r`.`autore` = `p`.`id`) "
        . "ORDER BY `r`.`data` DESC LIMIT " . $start . ", " . $num;
if ($data = DB_Manager::getInstance()->query($query)) {
    while ($row = $data->fetch_array(MYSQLI_ASSOC)) {
        $myTemplate->setBodyContentString("id", $row['id']);
        if (getLanguage() == 'it') {
            $myTemplate->setBodyContentString("titolo", $row['titolo']);
            $myTemplate->setBodyContentString("link_titolo", Config::$site_name . $prefix . $labelRecipe . '/' . $row['titolo'] . '/');
            $descrizione = $row['descrizione'];
        } else {
            $myTemplate->setBodyContentString("titolo", $row['titolo_en']);
            $myTemplate->setBodyContentString("link_titolo", Config::$site_name . $prefix . $labelRecipe . '/' . $row['titolo_en'] . '/');
            $descrizione = $row['descrizione_en'];
        }

        $myTemplate->setBodyContentString("tempo", $row['tempo']);
        $myTemplate->setBodyContentString("autore", $row['autore']);
        $myTemplate->setBodyContentImage("link_immagine", $row['uri']);
        $myTemplate->setBodyContentImage("link_immagine_thumb", Image_Manager::thumb($row['uri']));
        $difficolta = "";
        for ($i = 0; $i < $row['difficolta']; $i++) {
            $difficolta = $difficolta . '<i class="icon-star"></i>';
        }
        $myTemplate->setBodyContentString("difficolta", $difficolta);
        $myTemplate->setBodyContentString("persone", $row['persone']);
        $introlen = 200;

        $descr = (strlen($descrizione) > $introlen) ? substr($descrizione, 0, strpos($descrizione, ' ', $introlen)) . "..." : $descrizione;
        $myTemplate->setBodyContentString("descr", $descr);
    }
}
$s = ($pagina > $range) ? $pagina - $range : 0;
$list = "";
if ($pagina > 0) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelRecipes . '/' . $pagina . '/">' . L::messages_recipe_day_list_prev . '</a></li>';
}
for (; $s < $pagina; $s++) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelRecipes . '/' . ($s + 1) . '/">' . ($s + 1) . '</a></li>';
}
$list = $list . '<li><a class="current">' . ($s + 1) . '</a></li>';
$s++;
$end = (($pagina + $range) > $last) ? $last + 1 : ($pagina + $range) + 1;
for (; $s < $end; $s++) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelRecipes . '/' . ($s + 1) . '/">' . ($s + 1) . '</a></li>';
}
if ($pagina < $last) {
    $list = $list . '<li><a href="' . Config::$site_name . $prefix . $labelRecipes . ($pagina + 2) . '/">' . L::messages_recipe_day_list_next . '</a></li>';
}
$myTemplate->setBodyContentString("lista", $list);

$myTemplate->setBodyContentString("nav_message", L::nav_message);
$myTemplate->setBodyContentString("nav_home", L::nav_home);
$myTemplate->setBodyContentString("nav_page", L::messages_recipe_day_nav_page);

$myTemplate->setBodyContentString("nav_title", L::messages_recipe_day_nav_title);
$myTemplate->setBodyContentString("read_all", L::messages_recipe_day_read_all);
